package com.rwhansen.humidor;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class CigarBrandInformation {

	private ArrayList<String> brands;
	private ArrayList<String> sizes;
	private ArrayList<String> bodies;
	private ArrayList<String> countries;
	private ArrayList<String> purchaseLocations;
	private ArrayList<String> humidors;
	private ArrayList<String> caRatings;
	private ArrayList<String> caRatingYears;
	private ArrayList<String> fullySmoked;
	private ArrayList<String> wrapper;
	private ArrayList<String> wrapperOrigin;
	private ArrayList<String> filler;
	private ArrayList<String> fillerOrigin;
	private ArrayList<String> binder;
	private ArrayList<String> binderOrigin;

	public static final String CBI_FILE_NAME = "CigarBrandInformation.xml";

	

	public CigarBrandInformation() {
		brands = new ArrayList<String>();
		sizes = new ArrayList<String>();
		bodies = new ArrayList<String>();
		countries = new ArrayList<String>();
		purchaseLocations = new ArrayList<String>();
		humidors = new ArrayList<String>();
		caRatings = new ArrayList<String>();
		caRatingYears = new ArrayList<String>();
		fullySmoked = new ArrayList<String>();
	}
	
	
	public ArrayList<String> getWrapper() {
		return wrapper;
	}

	public ArrayList<String> getWrapperOrigin() {
		return wrapperOrigin;
	}

	public ArrayList<String> getFiller() {
		return filler;
	}

	public ArrayList<String> getFillerOrigin() {
		return fillerOrigin;
	}

	public ArrayList<String> getBinder() {
		return binder;
	}

	public ArrayList<String> getBinderOrigin() {
		return binderOrigin;
	}
	
	public ArrayList<String> getFinished() {
		return fullySmoked;
	}

	public ArrayList<String> getCaRatings() {
		return caRatings;
	}

	public ArrayList<String> getCaRatingYears() {
		return caRatingYears;
	}

	public ArrayList<String> getBrands() {
		return brands;
	}
	
	public void addBrand(String brand) {
	this.brands.add(brand);
	}

	public ArrayList<String> getSizes() {
		return sizes;
	}

	public void addSize(String size) {
		this.sizes.add(size);
	}

	public ArrayList<String> getBodies() {
		return bodies;
	}

	public void addBody(String body) {
		this.bodies.add(body);
	}
	
	public ArrayList<String> getCountries() {
		return countries;
	}

	public void addCountriy(String country) {
		this.countries.add(country);
		}

	public ArrayList<String> getPurchaseLocations() {
		return purchaseLocations;
	}

	public void addPurchaseLocation(String purchaseLocation) {
		this.purchaseLocations.add(purchaseLocation);
		}

	public ArrayList<String> getHumidors() {
		return humidors;
	}

	public void addHumidor(String humidor) {
		this.humidors.add(humidor);
		}

	
	
	private static boolean saveStringToFile(String fileName, String saveString) {
		boolean saved = false;
		BufferedWriter bw = null;
		
		try{
			bw = new BufferedWriter(new FileWriter(fileName));
			
			try {
				bw.write(saveString);
				saved = true;
			}
			finally {
				bw.close();
			}
		}
		catch (IOException ex) {
			ex.printStackTrace();
		}
		return saved;
	}

	private static String getStringFromFile(String fileName) {
		BufferedReader br = null;
		StringBuilder sb = new StringBuilder();
		
		try{
			br = new BufferedReader(new FileReader(fileName));
			try {
				String s;
				while ((s = br.readLine()) != null){
					//add linefeed back since stripped by readline()
					sb.append(s);
					sb.append("\n");
				}
			}
			finally {
				br.close();
			}
		}
		catch (IOException ex) {
			ex.printStackTrace();
		}
		return sb.toString();
	}

	private static String convertDatabaseToXML(CigarBrandInformation cbi) {
		XStream xStream = new XStream(new DomDriver());
		xStream.setMode(XStream.ID_REFERENCES);
		return xStream.toXML(cbi);
	}

	private static CigarBrandInformation convertXMLtoCigarBrandInformation(String XMLString) {
		CigarBrandInformation cbi = null;
		XStream xStream = new XStream(new DomDriver());
		xStream.setMode((XStream.ID_REFERENCES));
		Object obj = xStream.fromXML(XMLString);
		if (obj instanceof CigarBrandInformation)
		{
			cbi = (CigarBrandInformation) obj;
		}
		return cbi;
	}

	/**
	 * Commit cigar brand information to a file
	 * 
	 * @param fileName - the filename to save the cigar brand information to
	 * @param cbi - the cigar brand information object to save
	 * @return true if successful
	 */
	public boolean writeCigarBrandInformation(String fileName, CigarBrandInformation cbi) {
		return saveStringToFile(fileName, convertDatabaseToXML(cbi));
	}

	/**
	 * Read cigar brand information from file
	 * 
	 * @param fileName - the file to read the cigar brand information from
	 * @return the CigarBrandInformation object
	 */
	public static CigarBrandInformation readCigarBrandInformation(String fileName) {
		return convertXMLtoCigarBrandInformation(getStringFromFile(fileName));
	}
	
	public Object[] getComboBoxBodies(Cigar cigar) {
		ArrayList<String> bodyList = new ArrayList<String>();
		bodyList.add(cigar.getBody());
		bodyList.addAll(this.getBodies());
		return bodyList.toArray();
	}
	
	public Object[] getComboBoxBrands(Cigar cigar) {
		ArrayList<String> brandList = new ArrayList<String>();
		brandList.add(cigar.getBrand());
		brandList.addAll(this.getBrands());
		return brandList.toArray();
	}
	
	public Object[] getComboBoxSizes(Cigar cigar) {
		ArrayList<String> sizeList = new ArrayList<String>();
		sizeList.add(cigar.getSize());
		sizeList.addAll(this.getSizes());
		return sizeList.toArray();
	}
	
	public Object[] getComboBoxCountries(Cigar cigar) {
		ArrayList<String> countryList = new ArrayList<String>();
		countryList.add(cigar.getCountry());
		countryList.addAll(this.getCountries());
		return countryList.toArray();
	}
	
	public Object[] getComboBoxPurchaseLocations(Cigar cigar) {
		ArrayList<String> purchaseLocationList = new ArrayList<String>();
		purchaseLocationList.add(cigar.getPurchaseLocation());
		purchaseLocationList.addAll(this.getPurchaseLocations());
		return purchaseLocationList.toArray();
	}
	
	public Object[] getComboBoxHumidors(Cigar cigar) {
		ArrayList<String> humidorList = new ArrayList<String>();
		humidorList.add(cigar.getHumidor());
		humidorList.addAll(this.getHumidors());
		return humidorList.toArray();
	}

	public Object[] getComboBoxCARatings(Cigar cigar) {
		ArrayList<String> cARatingList = new ArrayList<String>();
		cARatingList.add(Integer.toString(cigar.getCaRating()));
		cARatingList.addAll(this.getCaRatings());
		return cARatingList.toArray();
	}

	public Object[] getComboBoxCARatingYears(Cigar cigar) {
		ArrayList<String> cARatingYearList = new ArrayList<String>();
		cARatingYearList.add(Integer.toString(cigar.getCaRatingYear()));
		cARatingYearList.addAll(this.getCaRatingYears());
		return cARatingYearList.toArray();
	}
	
	public Object[] getComboBoxFinished(Cigar cigar) {
		ArrayList<String> finishedList = new ArrayList<String>();
		finishedList.add(cigar.getFullySmoked());
		finishedList.addAll(this.getFinished());
//		finishedList.add("test");
		return finishedList.toArray();
	}
	
}
